const {getHttpCart} = require("../../models/http.js")
Page({
  data: {
    carts:[]
  },
  onLoad: function (options) {
    getHttpCart({
      success:res=>{
        var carts = res.data
        this.setData({
          carts
        })
      }
    })
  },
  onChange(event){
    this.getItemChange(event,"productCount")
  },
  onItemChange(event){
    this.getItemChange(event,"isSelected")
  },
  getItemChange(event,attr){
    var {id} = event.currentTarget.dataset
    var carts = this.data.carts
    var value = event.detail
    var item = carts.find(item=>item.id ==id)
    item[attr] = value
    this.setData({
      carts
    })
  },
  onSumChange(event){
    var carts = this.data.carts
    var value = event.detail
    carts.forEach(item=>{
       item.isSelected = value
    })
    this.setData({
      carts
    })
  }
})